
/**
* @Title: ItemServiceImpl.java
* @Package com.leetone.search.service.impl
* @Description: 【】
* @date 2018年4月3日 下午9:55:10
* @version V1.0
* @Company: 上海
* @author:[谭磊][leetone123@163.com]
*/

package com.leetone.search.service.impl;

import java.io.IOException;
import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.leetone.search.mapper.ItemMapper;
import com.leetone.search.pojo.Item;
import com.leetone.search.service.ItemService;
import com.leetone.utils.ExceptionUtils;
import com.leetone.utils.TaotaoResult;

/**
* @ClassName: ItemServiceImpl
* @Description: 【】
* @date 2018年4月3日 下午9:55:10
* @Company: 上海
* @author:[谭磊][leetone123@163.com]
*/
@Service("itemService")
public class ItemServiceImpl implements ItemService {
	@Autowired
	private ItemMapper itemMapper;
	@Autowired
	private SolrServer solrServer;
	@Override
	public TaotaoResult importAllItems() {
		List<Item> itemList = itemMapper.getItemList();
		try {
			for(Item item : itemList)
			{
				//创建一个SolrInputDocument对象
				SolrInputDocument document = new SolrInputDocument();
				document.setField("id", item.getId());
				document.setField("item_title", item.getTitle());
				document.setField("item_sell_point", item.getSellPoint());
				document.setField("item_price", item.getPrice());
				document.setField("item_image", item.getImage());
				document.setField("item_categroy_name", item.getCategoryName());
				document.setField("item_desc", item.getItemDes());
				//写入索引库
				solrServer.add(document);
			}
			solrServer.commit();
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtils.getStackTrace(e));
		}
		return TaotaoResult.ok();
	}

}
